//
//  QuotaListVC.swift
//  LeGou
//
//  Created by 刘亿前 on 2022/10/11.
//

import UIKit

class QuotaListVC: ZMViewController {
    
    lazy var listTable: UITableView = {
        let table = UITableView(frame: CGRect(), style: .plain)
        table.delegate = self
        table.dataSource = self
        table.separatorStyle = .none
        table.register(UINib(nibName: "MyRankingDetailCell", bundle: nil), forCellReuseIdentifier: "MyRankingDetailCell")
        table.register(UINib(nibName: "QuotaListCell", bundle: nil), forCellReuseIdentifier: "QuotaListCell")
        table.showsVerticalScrollIndicator = false
        table.tableFooterView = UIView()
        table.layer.cornerRadius = 12
        table.layer.masksToBounds = true
        view.addSubview(table)
        table.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalToSuperview()
            make.bottom.equalTo(-StaNavBarH)
//            make.height.equalTo(200)
//            make.height.equalTo(SCREEN_H - 12 - 44 - StaNavBarH)
        }
        addRefresh(scrollView: table) { [unowned self] refresh in
            self.page = refresh ?  1 : self.page + 1
            self.requestList()
        }
        table.mj_footer?.isHidden = true
        return table
    }()
    
    var page = 1
    let pageSize = 10
    var dataArr = [RankingDetailModel]()
    var index = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        glt_scrollView = listTable
        view.backgroundColor = "#F5F5F5".zm.hex()
        listTable.mj_header?.beginRefreshing()
        emptyImg = "nodata"
        emptyTitle = "暂无数据"
    }
}

extension QuotaListVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 68
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if index == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MyRankingDetailCell", for: indexPath) as! MyRankingDetailCell
            let info = dataArr[indexPath.row]
            cell.title.text = info.title
            cell.time.text = info.createTime
            cell.number.text = "+" + info.number.zm.toString(keep: 2)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "QuotaListCell", for: indexPath) as! QuotaListCell
            let info = dataArr[indexPath.row]
            cell.title.text = info.title
            cell.number.text = "已发放\(info.speed.zm.toString(keep: 2))%"
            cell.progress.progress = Float(info.speed / 100.0)
            cell.typeImg.image = UIImage(named: (info.monthTask ? "change_link_6" : "change_link_9"))
            return cell
        }
    }
}

// MARK: - Request Action
extension QuotaListVC {
    func requestList() {
        ZMLog(message: index)
        let pa = ["limit": pageSize, "page": page] as [String : Any]
        ZMUtil.listRequest(type: RankingDetailModel.self, api: (index == 0 ? MineAPI.listQuotaGive(pa) : MineAPI.listQuotaSurplus(pa))) { [unowned self]
            resp in
            if resp.code == .success, let data = resp.data {
                self.relodaData(data)
            } else {
                self.relodaData()
            }
        }
    }
    
    func relodaData(_ data: [RankingDetailModel] = []) {
        addNodataScrollView(scrollView: listTable)
        listTable.mj_header?.endRefreshing()
        listTable.mj_footer?.endRefreshing()
        if page == 1 {
            dataArr.removeAll()
        }
        dataArr.append(contentsOf: data)
        if data.count < pageSize {
            listTable.mj_footer?.endRefreshingWithNoMoreData()
        }
        listTable.mj_footer?.isHidden = dataArr.count == 0
        listTable.reloadData()
    }
}

